/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package it.kirys.rilego.gui;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.help.JHelp;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.help.HelpSetException;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JFrame;

/*
private static final String PATH_TO_JAVA_HELP =
"it/kirys/rilego/gui/rigogui-hs.xml"
;
 */
/**
 *
 * @author Federico Improta
 */
public class HelpAction extends AbstractAction {

	/**
	 * Constructor.
	 *
	 * @param aFrame parent window to which the help window is attached
	 * @param aText name of the menu item for this help action
	 * @param aMnemonicKeyEvent mnemonic for <tt>aText</tt>
	 * @param aIcon possibly-null graphic to be displayed alongside the text, or
	 * in a toolbar
	 * @param aView determines which help window is to be displayed: Contents, Index,
	 * or Search
	 */
	public HelpAction(JFrame aFrame, String aText, int aMnemonicKeyEvent, Icon aIcon, View aView) {
		super(aText, aIcon);
		/*		Args.checkForNull(aFrame);
		Args.checkForNull(aText);
		Args.checkForNull(aView);*/
		fFrame = aFrame;
		fView = aView;
		putValue(SHORT_DESCRIPTION, "StocksMonitor Help");
		putValue(LONG_DESCRIPTION, "Displays JavaHelp for StocksMonitor.");
		putValue(MNEMONIC_KEY, new Integer(aMnemonicKeyEvent));
		initHelpSystem();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		fLogger.info("Showing help system.");
/*		fHelpBroker.setCurrentView(fView.toString());
		fDisplayHelp.actionPerformed(event);**/
	}

	/** Enumeration for the style of presentation of the the Help system. */
	public enum View {

		SEARCH("Search"),
		CONTENTS("TOC"),
		INDEX("Index");

		@Override
		public String toString() {
			return fName;
		}

		private View(String aName) {
			fName = aName;
		}
		private String fName;
	}
	// PRIVATE //
	private JFrame fFrame;
	private View fView;
	/** Path used by a classloader to find the JavaHelp files. */
	//private static final String PATH_TO_JAVA_HELP ="/it/kirys/rilego/gui/rilegogui-hs.xml";
	private static final String PATH_TO_JAVA_HELP ="/guihelp/rilegogui-hs.xml";
	private ClassLoader DEFAULT_CLASS_LOADER = null;
	private static final Logger fLogger = Logger.getLogger(HelpAction.class.getCanonicalName());
	private HelpBroker fHelpBroker;
	private JHelp fDisplayHelp;

	/** Initialize the JavaHelp system. */
	private void initHelpSystem() {
		//optimization to avoid repeated init
		if (fHelpBroker != null && fDisplayHelp != null) {
			return;
		}

		//(uses the classloader mechanism)
		ClassLoader loader = this.getClass().getClassLoader();

		URL helpSetURL = HelpSet.findHelpSet(loader, PATH_TO_JAVA_HELP);
		helpSetURL=this.getClass().getResource(PATH_TO_JAVA_HELP);
		assert helpSetURL != null : "Cannot find help system.";
		try {
			HelpSet helpSet = new HelpSet(loader, helpSetURL);
			/*fHelpBroker = helpSet.createHelpBroker();
			fHelpBroker.enableHelpKey(fFrame.getRootPane(), "appwithhelp.about", helpSet);

			fDisplayHelp = new CSH.DisplayHelpFromSource(fHelpBroker);*/
			fDisplayHelp = new JHelp(helpSet);
			fDisplayHelp.setCurrentID("appwithhelp.about");
		} catch (HelpSetException ex) {
			fLogger.severe("Cannot create help system with: " + helpSetURL);
		}
		assert fHelpBroker != null : "HelpBroker is null.";
	}
}
